package com.itheima.springboottilasmanager.controller;

import com.itheima.springboottilasmanager.anonotation.log;
import com.itheima.springboottilasmanager.pojo.domain.PageResult;
import com.itheima.springboottilasmanager.pojo.domain.Result;
import com.itheima.springboottilasmanager.pojo.vo.StudentQuery;
import com.itheima.springboottilasmanager.pojo.domain.Student;
import com.itheima.springboottilasmanager.survice.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result findall(StudentQuery sq)
    {
    PageResult<Student> list = studentService.queryAll(sq);
        return Result.success(list);
    }
    @log
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
       log.info("批量删除员工{}",ids);
        studentService.deleteById(ids);
        return Result.success();
    }
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        boolean b = studentService.addStudent(student);
        return b?Result.success():Result.error("添加失败");
    }
    @log
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Student student = studentService.selectById(id);
        return student==null?Result.error("查询失败"):Result.success(student);
    }
    @PutMapping
    public Result updateStudent(@RequestBody Student student){

        boolean b = studentService.updateStudent(student);
        return b?Result.success():Result.error("修改失败");
    }
    @log
    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id,@PathVariable Double score){
         studentService.addViolation(id,score);
        return Result.success();
    }
}
